package com.miracle.java2110.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.miracle.java2110.service.OrderService;
import com.miracle.java2110.vo.OrderAddVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@Scope("prototype")
public class UploadOrderListener extends AnalysisEventListener<OrderAddVo> {
    @Autowired
    private OrderService orderService;

    private List<OrderAddVo> list = new ArrayList<>();

    public UploadOrderListener(OrderService orderService) {
        this.orderService = orderService;
    }

    public UploadOrderListener() {
    }

    //每读取一行数据 会调用一次这个方法
    @Override
    public void invoke(OrderAddVo orderAddVo, AnalysisContext analysisContext) {
        list.add(orderAddVo);
        System.out.println(orderAddVo);

    }

    //全部读取完之后 会调用该方法
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        String msg = orderService.uploadExcel(list).getMsg();
        System.out.println(msg);

        System.out.println("读取完毕");
    }
}